const router = require('koa-router')()

const fs = require('fs');




//获取登陆用户数据库投稿列表
router.get('/getUploaded', async (ctx, next) => {
    let openid = ctx.request.query.openid; 
    //获取前端传来的参数openid
   
    let sql ='select updraft.*, user_info.nickname,user_info.avatar from updraft left join user_info on updraft.openid = user_info.openid where updraft.openid=? '; 
    let reslist = await ctx.db.query(sql,[openid]); //将参数传入数组
    let sql2 = 'select draft_id from discovery_star'
    let res2 = await ctx.db.query(sql2)
    reslist.map(item=>{
      for(i=0;i<res2.length;i++){
        if(item.id==res2[i].draft_id){
          item.like_nums++
        }
      }
    })
    for(i=0;i<reslist.length;i++){
      if(reslist[i].img_name){
        reslist[i].img_name =fs.readFileSync('./public/images/uploadimgs/'+reslist[i].img_name).toString('base64')
      }
    }
    ctx.body={'code':200,'msg':'ok','draftList':reslist}
  })



  router.post('/deleteUploaded', async (ctx, next) => {
    const union_id=ctx.request.body.union_id
    try {
    await ctx.db.query('DELETE FROM updraft WHERE union_id = ?', [union_id]);
    ctx.body = { code: 200, msg: '删除成功' };
    } catch (e) {
    console.error(e);
    ctx.body = { code: 500, msg: '删除失败' };
    }
    });





module.exports = router